package com.smx.steppingstone.renderer;

import java.io.ByteArrayInputStream;

import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.smx.steppingstone.util.XMLUtil;

public abstract class AbstractRenderer {
	private  StringBuffer buffer = new StringBuffer();
	
	
	public void render(StringBuffer xmlstring) throws Exception{		
		render(xmlstring.toString());
	}
	
	public void render(String xmlstring) throws Exception{
		Document doc = XMLUtil.getXmlDocument(new ByteArrayInputStream(xmlstring.getBytes("UTF-8")));
		render(doc);
	}
	
	public void render(Document xmldocument) throws Exception{
		NodeList top = XMLUtil.extract("/instance", xmldocument);
		emit(top.item(0));
	}
	
	
	protected  void emit(Node root, boolean skipHeader, boolean skipAnonymous) throws Exception{
		NodeList topNodeList=null;
		topNodeList = XMLUtil.extract("./object", root);
		if(topNodeList.getLength()==0){
			topNodeList = XMLUtil.extract("./field", root);
			if(topNodeList.getLength()==0){
				topNodeList = XMLUtil.extract("./collection", root);
				if(topNodeList.getLength()==0){
					topNodeList = XMLUtil.extract("./array", root);
					emitArrayFields(topNodeList, null);	
				}else{
					emitCollectionFields(topNodeList, null);
				}					
			}else{
				emitFields(topNodeList, skipHeader, skipAnonymous);	
			}				
		}else{
			emitObject(topNodeList.item(0));
		}
	}
	protected  void emit(Node root) throws Exception{
		emit(root, false, false);
	}

	protected  void emitObject(Node node) throws Exception {		
		throw new RuntimeException("Method not implemented");
	}


	protected  void emitArrayFields(NodeList nodeList, Node owner)	throws Exception {

		throw new RuntimeException("Method not implemented");
	}
	protected  void emitCollectionFields(NodeList nodeList, Node owner)	throws Exception {
		throw new RuntimeException("Method not implemented");
	}

	protected  void emitMethods(NodeList nodeList) throws XPathExpressionException {
		throw new RuntimeException("Method not implemented");
	}

	protected  void emitFields(NodeList nodeList, boolean skipHeader, boolean skipName) {
		throw new RuntimeException("Method not implemented");
	}

	protected  void c(Object o) {
		buffer.append(o);
	}
	
	public StringBuffer getBuffer() {
		return buffer;
	}
}
